//防抖
export function debounce(fn, ms = 500) {
  let timeoutId
  return function () {
    clearTimeout(timeoutId)
    timeoutId = setTimeout(() => {
      fn.apply(this, arguments)
    }, ms)
  }
}

//节流
export function throttle(fn, timer) {
  let canRun = true
  return function () {
    if (!canRun) return
    canRun = false
    setTimeout(() => {
      fn.apply(this, arguments)
      canRun = true
    }, timer)
  }
}

//最后一个标签创建时间的处理函数
export function formatTagtime(time){
  if( time === undefined) return ''
  const timeArr = time.split(" ")
  const year = timeArr[0].split("-")[0]
  const month = timeArr[0].split("-")[1]*1
  const day = timeArr[0].split("-")[2]*1
  const hour = timeArr[1].split(":")[0]
  const minute = timeArr[1].split(":")[1]
  return `${month}月${day}日${hour}:${minute}`
}


// 从arr数组中随机获取 长度为ramNUm的子数组 子数组中元素不和lastArr中元素重复

export function randomPick(arr, ranNum, lastArr = []) {
  let arr2 = [].concat(arr)
  let result = []
  for (var i = 0; i < ranNum; i++) {
    let ran = Math.floor(Math.random() * arr2.length)
    let index = lastArr.indexOf(arr2[ran])
    if (index === -1) {
      result.push(arr2.splice(ran, 1)[0])
    } else {
      i--
    }
  }
  return result
}
